import API_BASE_URL from "../config";
import {getAction, postAction} from "@/manage";
import store from "@/store";
import {message} from "ant-design-vue";

//管理员信息
const root ={
    rootUserCode: '123456',
    rootUserId: '1',
    rootUserName: 'root'
}

//本地url
const url = {
    //获取会员信息
    getUserCode: `${API_BASE_URL}/get-user`,
    //获取管理员信息
    getUserName: `${API_BASE_URL}/get-info`,
    //获取wifi信息
    getWifi: `${API_BASE_URL}/get-wifi-info`,
    //获取电池信息
    getBatteryInfo: `${API_BASE_URL}/get-power`,
    //获取设备信息
    getDeviceInfo: `${API_BASE_URL}/get-device-info`,
    //获取设备号
    getSerial: `${API_BASE_URL}/get-serial`,
}

//云端url
const cloudUrl = {
    //设备号登录
    loginWithDeviceCode: "http://api.cgi-gz.com/login-store/with-serial",
    //获取管理员信息
    getUserName: `${API_BASE_URL}/get-info`,
    //获取wifi信息
    getWifi: `${API_BASE_URL}/get-wifi-info`,
    //获取电池信息
    getBatteryInfo: `${API_BASE_URL}/get-power`,

}

/**
 * 获取管理员信息
 */
export function getRootCode() {
    //初始化管理员编码、ID
    getAction(url.getUserCode, {card_number: '123456'}).then((res) => {
        store.commit('setRootUserCode', {value: res.data.data.card_number})
        store.commit('setRootId', {value: res.data.data.id})
        //设置当前用户为管理员
        store.commit('setUserCode', {value: store.state.rootUserCode})
        store.commit('setUserID', {value: store.state.rootId})
        console.log('管理员code:', store.state.rootUserCode)
        console.log('管理员id:', store.state.rootId)
        console.log('当前用户code:', store.state.userCode)
        console.log('当前用户id:', store.state.userId)
    })
}

/**
 * 获取管理员名称
 */
export function getRootName() {
    //初始化管理员名称
    getAction(url.getUserName).then((res) => {
        store.commit('setRootUserName', {value: res.data.data})
        store.commit('setUsername', {value: store.state.rootUserName})
        console.log('管理员name:', store.state.rootUserName)
        console.log('当前用户name:', store.state.username)
    })
}

/**
 * 获取wifi信息
 */
export function getWifiInfo() {
    //获取wifi信息
    getAction(url.getWifi).then((res) => {
        store.commit('setWifiName', {value: res.data.data.name})
        store.commit('setWifiSignal', {value: res.data.data.signal})
        console.log("wifi名称:", store.state.wifiName)
        console.log("wifi信号强度:", store.state.wifiSignal)
    }).catch(err => {
        console.log("获取wifi信息失败：", err)
    })
}

export function getCpuInfo() {
    getAction(url.getDeviceInfo).then((res) => {
        let cpuTemperature = res.data.data.cpu_temp
        store.commit('setCpuTemperature', {value: cpuTemperature})
        console.log("CPU温度:", cpuTemperature)
        if (cpuTemperature > 75) {
            message.warn("CPU温度过高！！！")
        }
    })
}

/**
 * 获取电池信息
 */
export function getBatteryInfo() {
    getAction(url.getBatteryInfo).then((res) => {
        let batteryCapacity = res.data.data.Percent
        console.log("当前电池状态：", res)
        if (res.data.data.status === 'Battery-PowerOn'){
            store.commit('setBatteryIsCharging',{value: false})
        }else {
            store.commit('setBatteryIsCharging',{value: true})
        }
        store.commit('setBatterySignal', {value: res.data.data.Percent})
        console.log("当前电池电量：", batteryCapacity)
    })
}

/**
 * 用设备号登录
 */
export function loginByDeviceCode() {
    postAction(cloudUrl.loginWithDeviceCode, {serial: store.state.deviceCode}).then((res) => {
        let data = res.data.data
        console.log("token:", data.token)
        store.commit('setToken', {value: data.token})
        store.commit('setStoreName', {value: data.store_name})
        store.commit('setStoreAccount', {value: data.store_account})
        console.log("storeName:", store.state.storeName)
        console.log("storeAccount:", store.state.storeAccount)
    }).catch(err => {
        console.log("登录失败：", err)
    })
}

/**
 * 获取设备信息
 */
export function getDeviceInfo() {
    getAction(url.getDeviceInfo).then((res) => {
        //总硬盘容量
        store.commit('setAllCapacity', {value: res.data.data.disk_info.all_capacity_G})
        //已使用硬盘容量
        store.commit('setUsedCapacity', {value: res.data.data.disk_info.used_capacity_G})
        //内存使用率
        store.commit('setRamUsage', {value: res.data.data.mem_info.mem_usage})
        //内存大小
        store.commit('setRamSize', {value: res.data.data.mem_info.mem_size_G})
        console.log("总硬盘容量:", store.state.allCapacity)
        console.log("已使用硬盘容量", store.state.usedCapacity)
        console.log("内存使用率", store.state.ramUsage)
        console.log("内存大小", store.state.ramSize)
    })
}

/**
 * 获取设备号
 */
export function getSerial() {
    getAction(url.getSerial).then((res) => {
        store.commit('setDeviceCode', {value: res.data.data.serial})
        console.log("设备号:", store.state.deviceCode)
    }).then(()=>{
        loginByDeviceCode()
    })
}

